package com.superbytecode.cloud.example.web.service;

import com.superbytecode.cloud.common.exception.BusinessException;
import com.superbytecode.cloud.common.model.IDict;
import com.superbytecode.cloud.example.web.dto.EmployeeVo;
import com.superbytecode.cloud.example.web.entity.Employee;
import com.superbytecode.cloud.example.web.enums.LevelEnum;
import com.superbytecode.cloud.example.web.service.convert.EmployeeConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.example.web.service
 * @ClassName: UserService
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/22 16:48
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class EmployeeService {
    final private static List<Employee> deptEmployeeMapper;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private EmployeeConvert employeeConvert;

    //
    public EmployeeVo queryEmployee(EmployeeVo vo) {
        // 模拟数据库查询基本类
        Employee employee = selectEmployeeByPrimary(vo.getId());
        List<Employee> teams = selectEmployeesForDept(vo.getDeptId());

        // convert 转换为VO
        EmployeeVo employeeVo = employeeConvert.toVo(employee);
        List<EmployeeVo> teamVos = new ArrayList<>();
        teams.forEach(t -> {
            teamVos.add(employeeConvert.toVo(t));
        });
        employeeVo.setTeams(teamVos);

        // 性别
        Integer sex = employeeVo.getSex();
        String sexValue = dictionaryService.obtainDictValue("sex", sex);
        employeeVo.setSexName(sexValue);
        // 部门
        Long deptId = employeeVo.getDeptId();
        String deptName = departmentService.obtainDeptName(deptId);
        employeeVo.setDeptName(deptName);
        // 岗位级别
        Integer level = employeeVo.getLevel();
        String levelName = IDict.obtainValueByCode(LevelEnum.class, level);
        employeeVo.setLevelName(levelName);
        return employeeVo;
    }


    // -----------------------------------------------------------------------------------------------------------------
    private List<Employee> selectEmployeesForDept(Long deptId) {
        return deptEmployeeMapper.stream().filter(e -> Objects.equals(deptId, e.getDeptId())).collect(Collectors.toList());
    }

    private Employee selectEmployeeByPrimary(Long empId) {
        return deptEmployeeMapper.stream().filter(e -> Objects.equals(empId, e.getId()))//
                .findAny().orElseThrow(() -> new BusinessException(""));
    }

    // 模拟数据库查询
    static {
        // MONITOR
        Employee monitor = Employee.init(1L, "tom", 1, 201L, LevelEnum.MONITOR.obtainCode());
        Employee director = Employee.init(10L, "lance ting", 1, 201L, LevelEnum.DIRECTOR.obtainCode());
        Employee zhangsan = Employee.init(100L, "zhangsan", 2, 201L, LevelEnum.MANAGER.obtainCode());
        deptEmployeeMapper = new ArrayList<Employee>() {{
            add(monitor);
            add(director);
            add(zhangsan);
        }};
    }

}